package com.road.vast.system.user.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.road.vast.pojo.system.PeRole;
import com.road.vast.pojo.system.PeUserRole;
import com.road.vast.system.component.IdWorkerComponent;
import com.road.vast.pojo.system.BsUser;
import com.road.vast.system.permission.mapper.PeUserRoleMapper;
import com.road.vast.system.role.mapper.PeRoleMapper;
import com.road.vast.system.user.mapper.BsUserMapper;
import com.road.vast.system.user.service.BsUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 郭泽鹏
 * @since 2020-07-13
 */
@Service
public class BsUserServiceImpl extends ServiceImpl<BsUserMapper, BsUser> implements BsUserService {
    @Resource
    BsUserMapper bsUserMapper;

    @Autowired
    IdWorkerComponent idWorkerComponent;

    @Resource
    PeRoleMapper peRoleMapper;

    @Resource
    PeUserRoleMapper peUserRoleMapper;

    @Resource
    PeUserRoleMapper userRoleMapper;

    @Override
    public Integer saveUser(BsUser bsUser) {
        String nextId = idWorkerComponent.getIdWorker().nextId() + "";
        bsUser.setId(nextId);
        bsUser.setPassword("123456");
        bsUser.setEnableState(1);
        return bsUserMapper.insert(bsUser);
    }

    @Override
    public Integer updateUser(BsUser bsUser) {
        return bsUserMapper.updateById(bsUser);
    }

    @Override
    public BsUser findById(String id) {
        return bsUserMapper.selectById(id);
    }

    @Override
    public List<String> simple(String id) {
        QueryWrapper<PeUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        List<PeUserRole> userRoles = peUserRoleMapper.selectList(wrapper);
        List<String> roleIds = new ArrayList<>();
        for (PeUserRole userRole : userRoles) {
            roleIds.add(userRole.getRoleId());
        }
        return roleIds;
    }

    @Override
    public Page<BsUser> findAll(Map<String, Object> map, int page, int size) {
        QueryWrapper<BsUser> wrapper = new QueryWrapper<>();
        //企业id
        if (!StringUtils.isEmpty((CharSequence) map.get("companyId"))){
        wrapper.eq("company_id", map.get("companyId"));
        }
        //部门id
        if (!StringUtils.isEmpty((CharSequence) map.get("departmentId"))){
            wrapper.eq("department_id", map.get("departmentId"));
        }
        //根据请求的hasDept判断，是否分配部门 0未分配（department = null）， 1 已分配（departmentId != null）
        if(!StringUtils.isEmpty((CharSequence) map.get("hasDept")))  {
            if("0".equals(map.get("hasDept"))){
                wrapper.isNull("department_id");
            }else{
                wrapper.isNotNull("department_id");
            }
        }
        //分页查询
        Page pageUser = new Page(page, size);
        return bsUserMapper.selectPage(pageUser, wrapper);
    }

    @Override
    public Integer deleteUser(String id) {
        return bsUserMapper.deleteById(id);
    }

    @Override
    public Integer assignRoles(String userId, List<String> roleIds) {
        //1.删除userId分配到的所有角色
        QueryWrapper<PeUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        Integer deleteUserRole = peUserRoleMapper.delete(wrapper);
        //2.为用户添加新的角色信息
        for (String roleId : roleIds) {
            String nextId = idWorkerComponent.getIdWorker().nextId() + "";
            PeUserRole userRole = new PeUserRole(nextId, userId, roleId);
            userRoleMapper.insert(userRole);
        }
        return 1;
    }
}
